package com.yc.boot.work.api;

import java.io.File;
import java.nio.file.Files;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.boot.common.model.ExcelAlias;
import com.yc.boot.common.model.response.Outcome;
import com.yc.boot.common.utils.ExcelUtil;
import com.yc.boot.work.entity.Test;
import com.yc.boot.work.model.find.TestPage;
import com.yc.boot.work.model.save.SaveTest;
import com.yc.boot.work.model.update.UpdateTest;
import com.yc.boot.work.service.TestService;

import cn.hutool.core.collection.CollUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Author:Yst
 * @Date:2021/11/10
 * @Time:13:37
 * @Info:
 */
@RestController
@Api(tags = "测试入口")
@RequestMapping("/test")
public class TestApi {

    @GetMapping("/file")
    @ApiOperation("Test文件copy")
    public Outcome<List<Test>> fileCopy() {
        try{
        File fileResource = new File("/resources/logback.xml");
        File newFile =new File("E:\\newFile\\");
        Files.copy(fileResource.toPath(),newFile.toPath());
        }catch (Exception e){
            e.printStackTrace();
        }
        return Outcome.success("zzzzzz");
    }

    @Resource
    private TestService TestService;

    @PostMapping
    @ApiOperation("Test-增")
    public Outcome save(@RequestBody @Valid SaveTest param) {
        return Outcome.status(TestService.saveWithoutId(param));
    }


    @DeleteMapping("/{id}")
    @ApiOperation("Test-删")
    public Outcome delete(@PathVariable Long id) {
        return Outcome.status(TestService.removeById(id));
    }

    @PutMapping
    @ApiOperation("Test-改")
    public Outcome update(@RequestBody @Valid UpdateTest param) {
        return Outcome.status(TestService.updateById(param));
    }

    @GetMapping("/all")
    @ApiOperation("Test-查-全部")
    public Outcome<List<Test>> queryAll() {
        return Outcome.success(TestService.list());
    }

    @GetMapping
    @ApiOperation("分页查询")
    public Outcome<IPage<Test>> queryByPage(TestPage param) {
        return Outcome.success(TestService.queryByPage(param));
    }

    @GetMapping("/queryById/{id}")
    @ApiOperation("Test-查-根据id")
    public Outcome queryById(@PathVariable Long id) {
        return Outcome.success(TestService.getById(id));
    }

    @GetMapping("/downExcl")
    @ApiOperation(value = "下载excl表格", produces = "application/octet-stream")
    public void downExcl(TestPage param) {
        IPage<Test> page = TestService.queryByPage(param);
        ExcelUtil.downExcel(page.getRecords(),
            CollUtil.newArrayList(ExcelAlias.makeAlias("id", "主键"),
                    ExcelAlias.makeAlias("createBy", "创建人"),
                ExcelAlias.makeAlias("createTime", "创建时间"),
                    ExcelAlias.makeAlias("updateBy", "修改人"),
                ExcelAlias.makeAlias("updateTime", "修改时间"),
                    ExcelAlias.makeAlias("isDelete", "是否删除"),
                ExcelAlias.makeAlias("name", "名称"),
                    ExcelAlias.makeAlias("status", "状态")));
    }

}
